package org.aplombh.java.utils.multiThread.juc.lock;

import java.util.concurrent.locks.ReentrantLock;

public class Lock implements Runnable {


    public static void main(String[] args) {
        Lock lock = new Lock();
        Thread thread1 = new Thread(lock);
        Thread thread2 = new Thread(lock);
        thread1.start();
        thread2.start();
    }

    private int ticketNums = 10;
    private final ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            try {
                lock.lock();
                if (ticketNums > 0) {
                    Thread.sleep(1000);
                    System.out.println(ticketNums--);
                } else
                    break;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
}
